#include "headers/idt.h"
#include "headers/pic.h"
#include "headers/isr.h"
#include "headers/irq.h"
#include "../api/headers/string.h"


/* An array containing the IDT entries */
idt_entry_type idt_entries[256];
isr_type interrupt_handlers[256];
/* A pointer to the IDT */
idt_pointer_type idt_pointer;

/* Loads the IDTR with the IDT pointer. */
void idt_flush(u32int idt_pointer);

void init_idt()
{
	idt_pointer.limit = sizeof(idt_entry_type) * 256 - 1;
	idt_pointer.base  = (u32int)&idt_entries;
	memset(&idt_entries, 0, sizeof(idt_entry_type) * 256);
	remap_pics();
	install_isrs();
	install_irqs();
	idt_flush((u32int)&idt_pointer);
	memset(&interrupt_handlers, 0, sizeof(isr_type) * 256);
}

void idt_set_gate(u8int number, u32int base, u16int selector, u8int flags)
{
	idt_entries[number].base_low = base & 0xFFFF;
	idt_entries[number].base_high = (base >> 16) & 0xFFFF;
	idt_entries[number].selector = selector;
	idt_entries[number].zero = 0;
	idt_entries[number].flags = flags;
}

void register_interrupt_handler(u8int interrupt_number, isr_type handler)
{
    interrupt_handlers[interrupt_number] = handler;
}
